package controllers

import (
	"fmt"
	"log"
	"loghub/cache"
	"loghub/db"
	"loghub/model"

	"github.com/kataras/iris/v12"
)

// RegisterLogController 是注册日志记录的控制器
func RegisterLogController(app *iris.Application) {
	// 记录一条完整的日志
	app.Post("/logdata", func(ctx iris.Context) {
		var pid = ctx.GetHeader("pid")
		projectInfo, err := cache.GetProjectInfo(pid)
		if err != nil {
			log.Println(err)
			ctx.JSON(model.ResponseData{State: false, Message: "获取项目失败", Error: err.Error(), HTTPCode: 400})
			return
		}
		if projectInfo.ProjectID != pid || len(pid) < 32 {
			ctx.JSON(model.ResponseData{State: false, Message: "获取项目失败", HTTPCode: 400})
			return
		}

		var logDetailData model.LogDetailData
		if err := ctx.ReadJSON(&logDetailData); err != nil {
			log.Println(err)
			ctx.JSON(model.ResponseData{State: false, Message: "读取参数失败", Error: err.Error(), HTTPCode: 400})
			return
		}

		if len(logDetailData.TraceID) < 32 {
			ctx.JSON(model.ResponseData{State: false, Message: "TraceID参数错误", HTTPCode: 400})
			return
		}
		// 写入日志到数据库中
		var sql = "insert into log_detail_data values ('%s','%s','%s','%s','%s','%d','%s','%s','%s','%s','%d','%s','%s','%s','%s','%s')"
		var querystring = fmt.Sprintf(sql,
			logDetailData.TraceID,
			logDetailData.ProjectID,
			logDetailData.LogLevel,
			logDetailData.LogTitle,
			logDetailData.LogContent,
			logDetailData.HTTPStatusCode,
			logDetailData.StackTrace,
			logDetailData.ErrorType,
			logDetailData.AssemblyName,
			logDetailData.ProcessName,
			logDetailData.ProcessID,
			logDetailData.PackageName,
			logDetailData.FunctionName,
			logDetailData.ClientIP,
			logDetailData.Remark,
			logDetailData.CreateTime.Format("2006-01-02 15:04:05"))
		exexResult, err := db.ExecSQL(querystring)
		if err != nil {
			log.Println(err)
			ctx.JSON(model.ResponseData{State: false, Message: "创建失败", Data: logDetailData, Error: err.Error(), HTTPCode: 500})
			return
		}
		fmt.Println(exexResult)
		ctx.JSON(model.ResponseData{State: true, Message: "写入日志成功"})
	})

	// 记录其中一条调用链路的详情
	app.Post("/logtrace", func(ctx iris.Context) {
		var pid = ctx.GetHeader("pid") // 验证项目是否存在,防止非法数据
		projectInfo, err := cache.GetProjectInfo(pid)
		if err != nil {
			log.Println(err)
			ctx.JSON(model.ResponseData{State: false, Message: "获取项目失败", Error: err.Error(), HTTPCode: 400})
			return
		}
		if projectInfo.ProjectID != pid || len(pid) < 32 {
			ctx.JSON(model.ResponseData{State: false, Message: "获取项目失败", HTTPCode: 400})
			return
		}

		var logTraceData model.LogTraceInfo
		if err := ctx.ReadJSON(&logTraceData); err != nil {
			log.Println(err)
			ctx.JSON(model.ResponseData{State: false, Message: "读取参数失败", Error: err.Error(), HTTPCode: 400})
			return
		}
		if len(logTraceData.TraceID) < 32 || len(logTraceData.RPCTraceID) < 32 {
			ctx.JSON(model.ResponseData{State: false, Message: "TraceID或者RPCTraceID参数错误", HTTPCode: 400})
			return
		}

		// 写入日志到数据库中
		var sql = "insert into log_trace_info values ('%s','%s','%s','%s','%s','%s','%s','%s','%d','%s','%s','%s','%s','%s','%s')"
		var querystring = fmt.Sprintf(sql,
			logTraceData.RPCTraceID,
			logTraceData.TraceID,
			logTraceData.LogTitle,
			logTraceData.LogContent,
			logTraceData.StackTrace,
			logTraceData.ErrorType,
			logTraceData.AssemblyName,
			logTraceData.ProcessName,
			logTraceData.ProcessID,
			logTraceData.PackageName,
			logTraceData.FunctionName,
			logTraceData.ClientIP,
			logTraceData.OSName,
			logTraceData.Remark,
			logTraceData.CreateTime.Format("2006-01-02 15:04:05"))
		exexResult, err := db.ExecSQL(querystring)
		if err != nil {
			log.Println(err)
			ctx.JSON(model.ResponseData{State: false, Message: "创建失败", Data: logTraceData, Error: err.Error(), HTTPCode: 500})
			return
		}
		fmt.Println(exexResult)
		ctx.JSON(model.ResponseData{State: true, Message: "写入日志成功"})
	})
}
